﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Xml;

namespace WSNKD07
{
    /// <summary>
    /// Summary description for Service1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class Service1 : System.Web.Services.WebService
    {      
        [WebMethod(EnableSession = true)]
        public XmlDocument loginNKD(string username, string password)
        {
            XmlDocument doc = new XmlDocument();
            SqlConnection cn = new SqlConnection();
            cn.ConnectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["ConnectionString"].ToString();

            SqlCommand cmd = new SqlCommand("SELECT * FROM Manufacturer WHERE username = @username", cn);
            cmd.Parameters.Add("@username", SqlDbType.VarChar);
            cmd.Parameters["@username"].Value = username;
            cn.Open();
            // DataSet ds = new DataSet(cmd, cn); 
            SqlDataReader rdr = cmd.ExecuteReader();
            rdr.Read();
            if (rdr.HasRows == false) return doc;
            if (password == rdr["password"].ToString())
            {
                XmlElement author = doc.CreateElement("key");
                author.InnerText = HttpContext.Current.Session.SessionID;
                doc.AppendChild(author);
                Session["key"] = HttpContext.Current.Session.SessionID;
                Session["user"] = username;
                Session["time"] = Convert.ToString(DateTime.Now.ToLocalTime());
                cn.Close();
                return doc;
            }
            else {
                XmlElement author = doc.CreateElement("key");
                author.InnerText = "false";
                doc.AppendChild(author);
                return doc;
            }
        }

        public int checkSession(string key, string username)
        {
            if (Session["key"].ToString() == key && Session["user"].ToString() == username)
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }

        [WebMethod(EnableSession = true)]
        public XmlDocument checkStock(string key, string user)
        {
            XmlDocument doc = new XmlDocument();
            if (1 == 1)
            {
               
                SqlConnection cn = new SqlConnection();
                XmlElement root = doc.CreateElement("root");
                doc.AppendChild(root);
                cn.ConnectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["ConnectionString"].ToString();

                SqlCommand cmd = new SqlCommand("SELECT * FROM Manufacturer WHERE username = @user", cn);
                cmd.Parameters.Add("@user", SqlDbType.VarChar);
                cmd.Parameters["@user"].Value = user;
                cn.Open();
                // DataSet ds = new DataSet(cmd, cn); 
                SqlDataReader rdr = cmd.ExecuteReader();                
                rdr.Read();
                if (rdr.HasRows == false) return doc;
                int idM = Convert.ToInt32(rdr["id"].ToString());
                cn.Close();

                cmd = new SqlCommand("SELECT * FROM ManufacturerContract,Product WHERE Product.id=ManufacturerContract.idProduct AND ManufacturerContract.idManufacturer = @idm AND ManufacturerContract.longTerm = 1 AND Product.stock <= ManufacturerContract.minStock ", cn);
                cmd.Parameters.Add("@idm", SqlDbType.Int);
                cmd.Parameters["@idm"].Value = idM;
                cn.Open();
                rdr = cmd.ExecuteReader();
                ArrayList returnData = new ArrayList();
                while (rdr.Read())
                {
                    XmlElement item = doc.CreateElement("item");
                    root.AppendChild(item);
                    
                    string tempData = rdr["name"].ToString() + "#" + rdr["stock"].ToString();
                    XmlElement id = doc.CreateElement("id");
                    id.InnerText = rdr["id"].ToString();
                    item.AppendChild(id);
                    XmlElement name = doc.CreateElement("name");
                    name.InnerText = rdr["name"].ToString();
                    item.AppendChild(name);
                    XmlElement stock = doc.CreateElement("stock");
                    stock.InnerText = rdr["stock"].ToString();
                    item.AppendChild(stock);
                }
                cn.Close();
                return doc;
            }
            else
            {
                return doc;
            }
        }

        [WebMethod(EnableSession = true)]
        public ArrayList productDetail(string key, string user, int idProduct)
        {
            if (1== 1)
            {
                SqlConnection cn = new SqlConnection();
                cn.ConnectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["ConnectionString"].ToString();

                //string QueryString = "SELECT * FROM Customer WHERE usename = '" + txtUsername.Text + "'";
                SqlCommand cmd = new SqlCommand("SELECT * FROM Product WHERE id = @id", cn);
                cmd.Parameters.Add("@id", SqlDbType.Int);
                cmd.Parameters["@id"].Value = idProduct;
                cn.Open();
                // DataSet ds = new DataSet(cmd, cn); 
                SqlDataReader rdr = cmd.ExecuteReader();
                rdr.Read();
                ArrayList DetailP = new ArrayList();
                DetailP.Add(rdr["id"].ToString());
                DetailP.Add(rdr["name"].ToString());
                DetailP.Add(rdr["imageUrl"].ToString());
                DetailP.Add(rdr["description"].ToString());
                DetailP.Add(rdr["idCAtegory"].ToString());
                DetailP.Add(rdr["stock"].ToString());
                return DetailP;
            }
            else { 
                ArrayList DetailP = new ArrayList();
                DetailP.Add(Session["key"].ToString());
                DetailP.Add(Session["user"].ToString());
                DetailP.Add(Session["time"].ToString());
                return DetailP;
            }
        }


        [WebMethod(EnableSession = true)]
        public int updateContract(string key, string username, float Iprice, int Inumber, int Ilongterm, string IdateBegin, string IdateEnd, int Imin, string confirm)
        {
            XmlDocument doc = new XmlDocument();
            if (1 == 1)
            {
                SqlConnection cn = new SqlConnection();
                XmlElement root = doc.CreateElement("root");
                doc.AppendChild(root);
                cn.ConnectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["ConnectionString"].ToString();


                SqlCommand cmd = new SqlCommand("SELECT * FROM ManufacturerContract WHERE confirm = @confirm", cn);
                cmd.Parameters.Add("@confirm", SqlDbType.NVarChar);
                cmd.Parameters["@confirm"].Value = confirm;
                cn.Open();
                // DataSet ds = new DataSet(cmd, cn); 
                SqlDataReader rdr = cmd.ExecuteReader();
                rdr.Read();
                string customerid = "";
                if (rdr.HasRows == false) return 0;               
                cn.Close();

                cmd = new SqlCommand("UPDATE ManufacturerContract SET price = @price, minStock = @min, min = @number, dateBegin = @dateBegin, dateEnd = @dateEnd, longTerm = @longterm WHERE confirm = confirm", cn);

                cmd.Parameters.Add("@price", SqlDbType.Float);
                cmd.Parameters["@price"].Value = Iprice;

                cmd.Parameters.Add("@number", SqlDbType.Int);
                cmd.Parameters["@number"].Value = Inumber;

                cmd.Parameters.Add("@longterm", SqlDbType.Int);
                cmd.Parameters["@longterm"].Value = Ilongterm;

                cmd.Parameters.Add("@product", SqlDbType.Int);
                cmd.Parameters["@product"].Value = Iproduct;

                cmd.Parameters.Add("@dateBegin", SqlDbType.Date);
                cmd.Parameters["@dateBegin"].Value = IdateBegin;

                cmd.Parameters.Add("@dateEnd", SqlDbType.Date);
                cmd.Parameters["@dateEnd"].Value = IdateEnd;

                cmd.Parameters.Add("@min", SqlDbType.Int);
                cmd.Parameters["@min"].Value = Imin;

                cn.Open();
                // DataSet ds = new DataSet(cmd, cn); 
                int result = cmd.ExecuteNonQuery();
                cn.Close();
                return result;
            }
            else
            {
                return 0;
            }
        }
       
    }
}
